//********************************************************************************************************
// Product Name: MapWindow.dll Alpha
// Description:  The basic module for MapWindow version 6.0
//********************************************************************************************************
// The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); 
// you may not use this file except in compliance with the License. You may obtain a copy of the License at 
// http://www.mozilla.org/MPL/ 
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 
// ANY KIND, either expressed or implied. See the License for the specificlanguage governing rights and 
// limitations under the License. 
//
// The Original Code is from MapWindow.dll version 6.0
//
// The Initial Developer of this Original Code is Ted Dunsford. Created 6/25/2008 10:28:22 AM
// 
// Contributor(s): (Open source contributors should list themselves and their modifications here). 
//
//********************************************************************************************************

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;

using MapWindow.Geometries;
using MapWindow.Drawing;

namespace MapWindow.DirectX
{


    /// <summary>
    /// TexturedLineString
    /// </summary>
    public class ColoredSegmentList: PrimitiveBaseDX
    {
        #region Private Variables

        double _width;

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new instance of TexturedLineString
        /// </summary>
        public ColoredSegmentList(Color color, float width):base(color)
        {
            _width = width;
        }

       

        #endregion

        #region Methods

       
        /// <summary>
        /// Adds a new segment to this list of segments
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        public void AddSegment(Coordinate start, Coordinate end)
        {
            double sZ = start.Z;
            if (double.IsNaN(sZ)) sZ = 0.0;
            double eZ = end.Z;
            if (double.IsNaN(eZ)) eZ = 0.0;
            Vector vStart = new Vector(start.X, start.Y, sZ);
            Vector vEnd = new Vector(end.X, end.Y, eZ);
            Vector up = new Vector(0, 0, 1);
            Vector dir = vEnd.Subtract(vStart);
            Vector offsetDir = up.Cross(dir);
            offsetDir.Normalize();


            Vector w = offsetDir.Multiply( _width / 2);

            Vertices.AddCoordinate(vStart.Add(w));
            Vertices.AddCoordinate(vEnd.Add(w));
            Vertices.AddCoordinate(vStart.Subtract(w));
            Vertices.AddCoordinate(vEnd.Subtract(w));


            //float sZ = Convert.ToSingle(start.Z);
            //if (float.IsNaN(sZ)) sZ = 0F;
            //float eZ = Convert.ToSingle(end.Z);
            //if (float.IsNaN(eZ)) eZ = 0F;
            //Vector3 vStart = new Vector3(Convert.ToSingle(start.X), Convert.ToSingle(start.Y), sZ);
            //Vector3 vEnd = new Vector3(Convert.ToSingle(end.X), Convert.ToSingle(end.Y), eZ);  
            //Vector3 up = new Vector3(0, 0, 1);
            //Vector3 dir = Vector3.Subtract(vEnd, vStart);
            //Vector3 offsetDir = Vector3.Cross(up, dir);
            //offsetDir.Normalize();
           

            //Vector3 w = Vector3.Multiply(offsetDir, _width / 2);
            
            //Vertices.AddCoordinate(Vector3.Add(vStart, w));
            //Vertices.AddCoordinate(Vector3.Add(vEnd, w));
            //Vertices.AddCoordinate(Vector3.Subtract(vStart, w));
            //Vertices.AddCoordinate(Vector3.Subtract(vEnd, w));


        }

        ///// <summary>
        ///// Assigns the index values to match the number of vertices
        ///// </summary>
        ///// <param name="args"></param>
        //public override void Initialize(DrawArgsDX args)
        //{
        //    if (Indices == null || Indices.Count == 0)
        //    {
        //        Indices = new IndexCollection(Vertices.Count / 4);
        //    }
        //    IsInitialized = true;
        //}

        #endregion

        #region Properties


        /// <summary>
        /// Gets or sets the float width to use when calculating the size of each segment.
        /// </summary>
        public virtual double Width
        {
            get { return _width; }
            set { _width = value; }
        }

       
        #endregion

        #region Protected Methods

        /// <summary>
        /// Initializes the indices if they haven't been initialized yet.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInitialize(DrawArgsDX e)
        {
            if (Indices == null || Indices.Count == 0)
            {
                Indices = new IndexCollection(Vertices.Count / 4);
            }
            base.OnInitialize(e);
        }

        #endregion

    }
}
